<?php

namespace api\modules\v1\controllers;

use yii;
use ServerAPI;
use yii\web\Response;
use yii\web\Controller;
use api\models\ZhiboHistory;
use api\models\ZhiboList;
use api\models\User;



/**
 * Default controller for the `v1` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
//        try{
//            if ($_POST) {
        $url = \Yii::$app->request->get('hlsPullUrl');
        $result = ZhiboList::find()->where(['hlsPullUrl' => $url])->one();
        $userId = $result['user_id'];
        $title = $result['title'];
        $region = $result['region'];
        $cid = $result['cid'];
        $result1 = User::find()->where(['user_id' => $userId])->one();
        $icon = $result1['icon'];
        $signature = $result1['signature'];
        $username = $result1['username'];
        $p = new ServerAPI();
        $info = $p->getStatus($cid);
//            var_dump($info);
        if ($info['ret']['status'] == 0) {
            $status = 0;
        }
        if ($info['ret']['status'] == 3){
            $status = 1;
        }



        $type = "application/x-mpegURL";
        $session = Yii::$app->session;
        $session->set('url', $url);
//            $url = $_GET[]; //=         "http://pullhls221a204b.live.126.net/live/30735febc99449f68809a8d07dbabd31/playlist.m3u8";
//            $url = yii\helpers\ArrayHelper::json_var(200, 'chengg ', $url);
//            $url = json_encode($url);
//                $session = Yii::$app->session;
//                $url = $session->get('url');
//        Yii::$app->response->format=Response::FORMAT_JSON;
//        return $url;
        $url = $session->get('url');

//        var_dump($url);
        return $this->render('index', ['url' => $url, 'type' => $type, 'icon' => $icon, 'signature' =>$signature, 'username' => $username, 'region' => $region, 'title' => $title, 'status' => $status]);
//            } else {

//            }
//
//        } catch (\Exception $e) {
//            $e->getMessage();
//        }

    }

    public function actionMp4player()
    {
        $type = "video/mp4";
//        $url = \Yii::$app->request->get();
        $id = $_GET['id'];
//        var_dump($id);
        $result = ZhiboHistory::find()->where(['id' =>$id])->one();
        $url = $result['hdMp4Url'];
        $userId = $result['user_id'];
        $title = $result['title'];
        $region = $result['region'];
//        var_dump($region);
        $result1 = User::find()->where(['user_id' => $userId])->one();
        $icon = $result1['icon'];
        $signature = $result1['signature'];
        $username = $result1['username'];

        return $this->render('index', ['url' => $url, 'type' => $type, 'icon' => $icon, 'signature' =>$signature, 'username' => $username, 'region' => $region, 'title' => $title]);

    }












}
